package com.daxia.bumall.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daxia.bumall.dao.FactoryInfoDAO;
import com.daxia.bumall.dto.FactoryInfoDTO;
import com.daxia.bumall.model.FactoryInfo;
import com.daxia.bumall.support.Page;
import com.daxia.bumall.util.BeanMapper;

@Service
public class FactoryInfoService {
	
	@Autowired
	private FactoryInfoDAO factoryInfoDAO;
	
	public List<FactoryInfoDTO> find(FactoryInfoDTO query, Page page) {
		List<FactoryInfo> models = factoryInfoDAO.find(query, page);
		List<FactoryInfoDTO> dtos = toDTOs(models);
		return dtos;
	}
	
	public List<FactoryInfoDTO> findAll() {
		return this.find(new FactoryInfoDTO(), null);
	}
	
	public List<FactoryInfoDTO> find(int count) {
		Page page = new Page();
		page.setPage(1);
		page.setPageSize(count);
		return this.find(new FactoryInfoDTO(), page);
	}
	
	public Long create(FactoryInfoDTO dto) {
		FactoryInfo model = new FactoryInfo();
		toModel(model, dto);
		return factoryInfoDAO.create(model);
	}
	
	public FactoryInfoDTO load(Long id) {
	    FactoryInfo model = factoryInfoDAO.load(id);
	    return toDTO(model);
    }

	public void updateAllFields(FactoryInfoDTO dto) {
		FactoryInfo model = factoryInfoDAO.load(dto.getId());
		toModel(model, dto);
		factoryInfoDAO.update(model);
    }

	public void deleteByIds(Long[] ids) {
		if (ids != null) {
			for (Long id : ids) {
				factoryInfoDAO.deleteById(id);
			}
		}
    }
	
	public FactoryInfoDTO findFactoryByUserId(Long userId) {
		FactoryInfo factoryInfo = factoryInfoDAO.findFactoryByUserId(userId);
		return toDTO(factoryInfo);
	}
	
	public void deleteById(Long id) {
		this.deleteByIds(new Long[] {id});
	}

	public FactoryInfoDTO findOne(FactoryInfoDTO query) {
		FactoryInfo model = factoryInfoDAO.findOne(query);
		return toDTO(model);
	}
	
	private List<FactoryInfoDTO> toDTOs(List<FactoryInfo> models) {
		if (CollectionUtils.isEmpty(models)) {
			return new ArrayList<FactoryInfoDTO>(0);
		}
		List<FactoryInfoDTO> dtos = new ArrayList<FactoryInfoDTO>(models.size());
		for (FactoryInfo model : models) {
	        FactoryInfoDTO dto = toDTO(model);
	        dtos.add(dto);
        }
	    return dtos;
    }
    
    private FactoryInfoDTO toDTO(FactoryInfo model) {
		if (model == null) {
			return null;
		}
		FactoryInfoDTO dto = BeanMapper.map(model, FactoryInfoDTO.class);
		
		return dto;
	}
	
	private void toModel(FactoryInfo model, FactoryInfoDTO dto) {
		BeanMapper.copy(dto, model);
    }
	
	public int updateFactoryStatus(Long factoryId,Integer status) {
		return factoryInfoDAO.updateFactoryStatus(factoryId, status);
	}
	
	@SuppressWarnings("unused")
	private List<FactoryInfo> toModels(List<FactoryInfoDTO> dtos) {
		if (CollectionUtils.isEmpty(dtos)) {
			return new ArrayList<FactoryInfo>(0);
		}
		List<FactoryInfo> models = new ArrayList<FactoryInfo>(dtos.size());
		for (FactoryInfoDTO dto : dtos) {
	        FactoryInfo model = new FactoryInfo();
	        toModel(model, dto);
	        models.add(model);
        }
		return models;
	}

	public FactoryInfoDTO findByExactName(String factoryName) {
		FactoryInfoDTO query = new FactoryInfoDTO();
		query.setExactName(factoryName);
		return this.findOne(query);
	}
}
